package com.gitee.feizns.web.boot.ex;

import com.gitee.feizns.web.domain.result.Err;
import com.gitee.feizns.web.ex.CustomizeExceptionHandler;
import com.gitee.feizns.web.ex.HttpException;
import com.gitee.feizns.web.ex.MessageException;
import com.gitee.feizns.web.ex.ServiceException;
import com.gitee.feizns.web.ex.impl.BindExceptionHandler;
import com.gitee.feizns.web.ex.impl.ConstraintViolationExceptionHandler;
import com.gitee.feizns.web.ex.impl.HttpExceptionHandler;
import com.gitee.feizns.web.ex.impl.HttpMediaTypeNotSupportedExceptionHandler;
import com.gitee.feizns.web.ex.impl.HttpMessageNotReadableExceptionHandler;
import com.gitee.feizns.web.ex.impl.HttpRequestMethodNotSupportedExceptionHandler;
import com.gitee.feizns.web.ex.impl.MessageExceptionHandler;
import com.gitee.feizns.web.ex.impl.MethodArgumentNotValidExceptionHandler;
import com.gitee.feizns.web.ex.impl.ServiceExceptionHandler;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;

import javax.validation.ConstraintViolationException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author feizns
 * @since 2019/6/13
 */
@Data
@ConfigurationProperties("commons.web.ex.global")
public class GlobalExceptionProperties {

    /**
     * 是否是调试模式：调试模式 将打印 Throwable.printStackTrace().
     */
    private boolean debug;

    /**
     * 异常处理映射器
     */
    private Map<Class<?>, Class<? extends CustomizeExceptionHandler>> exceptionHandlerMap = new LinkedHashMap<>();

    {
        exceptionHandlerMap.put(ServiceException.class, ServiceExceptionHandler.class);
        exceptionHandlerMap.put(HttpException.class, HttpExceptionHandler.class);
        exceptionHandlerMap.put(BindingResult.class, BindExceptionHandler.class);
        exceptionHandlerMap.put(HttpMessageNotReadableException.class, HttpMessageNotReadableExceptionHandler.class);
        exceptionHandlerMap.put(MethodArgumentNotValidException.class, MethodArgumentNotValidExceptionHandler.class);
        exceptionHandlerMap.put(HttpRequestMethodNotSupportedException.class, HttpRequestMethodNotSupportedExceptionHandler.class);
        exceptionHandlerMap.put(ConstraintViolationException.class, ConstraintViolationExceptionHandler.class);
        exceptionHandlerMap.put(HttpMediaTypeNotSupportedException.class, HttpMediaTypeNotSupportedExceptionHandler.class);
        exceptionHandlerMap.put(MessageException.class, MessageExceptionHandler.class);
    }

    /**
     * 异常类映射错误码
     */
    private Map<Class<? extends Throwable>, Err> errMap = new LinkedHashMap<>();

}
